<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>注册</title>
  <style>
    body {
      background: linear-gradient(135deg, #f5f7fa 0%, #cad2e0 100%);
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }

    .register-container {
      background-color: rgba(255, 255, 255, 0.9);
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      width: 300px;
      text-align: center;
    }

    .register-container h1 {
      margin-bottom: 20px;
      font-family: 'Arial', sans-serif;
      color: #333;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
      animation: fadeIn 1s ease-in-out;
    }

    .register-container .form-group {
      margin-bottom: 15px;
      text-align: left;
    }

    .register-container .form-group label {
      display: inline-block;
      width: 100px;
      text-align: right;
      margin-right: 10px;
    }

    .register-container .form-group input[type="text"],
    .register-container .form-group input[type="password"] {
      width: 150px;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    .register-container button {
      width: 80px;
      padding: 8px;
      margin: 5px;
      border: none;
      border-radius: 4px;
      background-color: #007bff;
      color: white;
      cursor: pointer;
    }

    .register-container button:hover {
      background-color: #0056b3;
    }

    .error-message {
      color: red;
      margin-top: 10px;
    }

    @keyframes fadeIn {
      from { opacity: 0; }
      to { opacity: 1; }
    }
  </style>
  <script>
    document.getElementById('username').addEventListener('blur', checkUsernameAvailability);

    async function register() {
      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const confirmPassword = document.getElementById('confirmPassword').value;

      if (password !== confirmPassword) {
        document.querySelector('.error-message').textContent = '两次密码不一致';
        return;
      }

      const response = await fetch('/api/register', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ username, password })
      });

      if (response.ok) {
        window.location.href = 'login.html';
      } else {
        const error = await response.text(); // 直接获取文本内容
        document.querySelector('.error-message').textContent = error;
      }
    }

  </script>
</head>
<body>
<div class="register-container">
  <h1>广告主注册</h1>
  <form onsubmit="event.preventDefault(); register();">
    <div class="form-group">
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username" required>
    </div>
    <div class="form-group">
      <label for="password">密码:</label>
      <input type="password" id="password" name="password" required>
    </div>
    <div class="form-group">
      <label for="confirmPassword">确认密码:</label>
      <input type="password" id="confirmPassword" name="confirmPassword" required>
    </div>

    <div style="text-align: center;">
      <button type="submit">注册</button>
      <button type="button" onclick="location.href='login.html'">返回</button>
    </div>
    <div class="error-message"></div>
  </form>
</div>
</body>
</html>
